/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet.medic;

import Logger.MyLogger;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author giovanni
 */
public class Richiamo extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        MyLogger.getLogger().debug("Servlet per effettuare il richiamo dei pazienti");
        PrintWriter out = response.getWriter();
        try {

            out.println("<div id=\"dialog-modal\" title=\"Info Paziente\"><!-- Div usato per visualizzare le info pazienti --></div>"
                    + "<h2>Procedura per il richiamo dei pazienti</h2>"
                    + "In questa pagina è possibile visualizzare e successivamente richiamare i pazienti vaccinati prima"
                    + "della data indicata.<br/>"
                    + "Soglia per i richiami: <input type=\"text\" name=\"datepicker\" id=\"datepicker\" size=\"9\"/>"
                    + "che corrisponde a <input type=\"text\" id=\"diff_giorni\" size=\"4\"/> giorni fa."
                    + "<form action=\"CreaPdf\">"
                    + "<table class=\"border maxwidth\">"
                    + "<thead>"
                    + "<tr>"
                    + "<th>Nome Paziente</th>"
                    + "<th>Data nascita</th>"
                    + "<th>Medico</th>"
                    + "<th>Ultima vaccinazione</th>"
                    + "<th>Scelto</th>"
                    + "</tr>"
                    + "</thead>"
                    + "<tbody><!-- Caricato tramite ajax ogni volta che si cambia il giorno --></tbody>"
                    + "</table>"
                    + "<input type=\"submit\" value=\"Richiama Pazienti e crea il file PDF\">"
                    + "</form>"
                    + "<script>"
                    + "$(\"#datepicker\").datepicker({"
                    + "changeMonth: true,"
                    + "changeYear: true,"
                    + "maxDate: \"0D\""
                    + "});    "
                    + "$(\"#datepicker\").change(function(){"
                    + "var today = new Date().getTime();"
                    + "var ago = new Date($(this).val()).getTime();"
                    + "var diff = new Date(Math.abs(today-ago)).getTime();"
                    + "$(\"#diff_giorni\").val(Math.floor(diff/(1000*3600*24)));"
                    + "$.ajax({"
                    + "url: \"ListaPazienti\","
                    + "data: {data: $(\"#datepicker\").val()},"
                    + "dataType: \"html\","
                    + "success: function (data, status, xhr){"
                    + "$(\"tbody\").html(data);"
                    + "}"
                    + "});"
                    + "});"
                    + "function mostra_info(id){"
                    + "$( \"#dialog-modal\" ).dialog({"
                    + "height: 300,"
                    + "width: 300,"
                    + "modal: true"
                    + "});"
                    + "$.ajax({"
                    + "url: \"Mostra_paziente.jsp\","
                    + "dataType: \"html\","
                    + "data: {Id: id},"
                    + "success: function(data){"
                    + "$( \"#dialog-modal\" ).html(data);"
                    + "}"
                    + "});"
                    + "}"
                    + "</script>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
